package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketAddress;
import com.cskaoyan.market.db.domain.MarketAddressExample;
import com.cskaoyan.market.db.mapper.MarketAddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class WxAddressServiceImpl implements WxAddressService {
    @Autowired
    MarketAddressMapper addressMapper;

    @Override
    public List<MarketAddress> list(Integer userId) {
        MarketAddressExample addressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        return addressMapper.selectByExample(addressExample);
    }

    @Transactional
    @Override
    public int save(MarketAddress marketAddress) {
        Integer addressId = marketAddress.getId();

        int data;
        marketAddress.setUpdateTime(LocalDateTime.now());
        marketAddress.setDeleted(false);
        MarketAddressExample addressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andIdEqualTo(addressId);
        //默认地址只能有一个
        if (marketAddress.getIsDefault()) {
            //如果该对象的is_default字段为true，则修改其他地址的is_default字段为false
            MarketAddress updateDefault = new MarketAddress();
            updateDefault.setIsDefault(false);
            MarketAddressExample example = new MarketAddressExample();
            MarketAddressExample.Criteria exampleCriteria = example.createCriteria();
            //更该其他为
            exampleCriteria.andIsDefaultEqualTo(true);
            addressMapper.updateByExampleSelective(updateDefault, example);
        }
        if (addressMapper.selectByPrimaryKey(addressId) != null) {
            addressMapper.updateByExample(marketAddress, addressExample);
            data = addressId;//data为当前地址的id
        }
        else {
            marketAddress.setAddTime(LocalDateTime.now());
            data = addressMapper.insertSelective(marketAddress);//若为新增的地址，则data为最新的主键值
        }
        return data;
    }

    @Override
    public MarketAddress detail(Integer id) {
        MarketAddressExample addressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andIdEqualTo(id);
        return addressMapper.selectOneByExampleSelective(addressExample);
    }

    @Override
    public boolean delete(Integer id) {
        MarketAddressExample addressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andIdEqualTo(id);
        if (addressMapper.selectOneByExample(addressExample) != null) {
            //此处也可以设置deleted标记为true代表删除
            addressMapper.deleteByExample(addressExample);
            return true;
        }
        return false;
    }

}

